# -*- coding: utf-8 -*-

import sys

from datetime import datetime as dt
import datetime

#-------------project bag---------------
from log import logger


#return time format,list: year,month,day,hour,minute,seconds

def time_now():
	return dt.now()

def local_time(type = 'datetime'):     #本地时间
    if type == 'datetime':      #时分秒
        current_time = (datetime.datetime.now()-datetime.timedelta(minutes=0)).strftime("%Y-%m-%d %H:%M:%S")
    if type == 'date':
        current_time = (datetime.datetime.now()-datetime.timedelta(minutes=0)).strftime("%Y-%m-%d")
    if type == 'time':
        current_time = (datetime.datetime.now()-datetime.timedelta(minutes=0)).strftime("%H:%M:%S")
    return current_time

def date_time(type = 'year'): #年月日时分秒毫秒
    now = dt.now()
    if type == 'year':
        return int(now.year)
    if type == 'month':
        return int(now.month)
    if type == 'day':
        return int(now.day)
    if type == 'hour':
        return int(now.hour)
    if type == 'minute':
        return int(now.minute)
    if type == 'second':
        return int(now.second)

def date_now():      #返回[year,month,day,hour,minute,seconds]
    now = dt.now()
    return [now.year,now.month,now.day,now.hour,now.minute,now.second]

def fix_time_format(string):    #返回时间格式2000-01-01
    if not len(string) == 8:
        print u'请输入正确格式：20160101'
        return 0
    year = string[0:4]
    month = string[4:6]
    day = string[6:8]
    date = year+'-'+month+'-'+day
    return date

def get_time_total_seconds(time_list):     #[hours,minutes,seconds]
	total_seconds = int(total_seconds[0])*60*60+int(total_seconds[1])*60+int(total_seconds[0])
	return total_seconds


def datetime_diff(date_list_end,date_list_start):   #[year,month,day,hour,minute,seconds],返回秒数
    diff_list = []
    if not len(date_list_start) == 6:
        return u'the length of date_list must be 6'
    if not len(date_list_start) == len(date_list_end):
        return u'the num is diffence between the two [date_list]'
    date_start = '%d-%d-%d %d:%d:%d' % (int(date_list_start[0]),int(date_list_start[1]),int(date_list_start[2]),int(date_list_start[3]),int(date_list_start[4]),int(date_list_start[5]))
    date_end = '%d-%d-%d %d:%d:%d' % (int(date_list_end[0]),int(date_list_end[1]),int(date_list_end[2]),int(date_list_end[3]),int(date_list_end[4]),int(date_list_end[5]))
    start_date = dt.strptime(date_start,'%Y-%m-%d %H:%M:%S')
    end_date = dt.strptime(date_end,'%Y-%m-%d %H:%M:%S')
    diff = end_date - start_date
    diff_seconds = diff.total_seconds()
    return int(diff_seconds)

def time_diff(time_list_start,time_list_end):   #[hours,minute,seconds]
	if not len(time_list_start)==3 or len(time_list_end)==3:
		logger.error('two args must be length 3')
		return 0
	if time_list_start[0] > 24 or time_list_start[0] <0 or time_list_end[0] > 24 or time_list_end[0] <0:
		logger.error('hours must be 24')
		return 0
	start_time_seconds = get_time_total_seconds(time_list_start)
	end_time_seconds = get_time_total_seconds(time_list_end)
	diff_seconds = end_time_seconds - start_time_seconds
	return diff_seconds

def day_diff(date_list_start,date_list_end):
	d_start = dt(date_list_start[0],date_list_start[1],date_list_start[2])
	d_end = dt(date_list_end[0],date_list_end[1],date_list_end[2])
	diff_day = (d_end - d_start).days
	return diff_day


def time_delay(seconds):     #根据秒数时间差，返回天数，时分秒，输出列表
    year_days = 365
    delay =[]
    second = seconds%60
    minute = (seconds/60)%60
    hour = (seconds/(60*60))%24
    day = (seconds/(60*60*24))% year_days
    year = seconds/(60*60*24*year_days)
    if not year == 0:
        delay = [int(year),int(day),int(hour),int(minute),int(second)]
    else:
        delay = [int(day),int(hour),int(minute),int(second)]
    return delay

def time_delay_dict(seconds):     #根据秒数时间差，返回天数，时分秒,输出字典
    delay = {}
    date = time_delay(seconds)
    if len(date) == 5:
        li = ['year','day','hour','minute','second']
    if len(date) == 4:
        li = ['day','hour','minute','second']
    for i in xrange(len(li)):
        delay[li[i]] = date[i]
    return delay